package client;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.HeadlessException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;

import java.awt.Dialog.ModalityType;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.swing.JLabel;
import javax.swing.JTextField;

import server.PersonInterface;



public class RegGUI extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField_ID;
	private JPasswordField textField_pw;
	private JPasswordField textField_pw2;
	private JTextField textField_question;
	private JTextField textField_answer;
	private JTextField textField_name;
	private JTextField textField_sex;
	private JButton okButton;
	private JButton cancelButton;

	/**
	 * Launch the application.
	 */

	/*public static void main(String[] args) {
		try {
			RegGUI dialog = new RegGUI(this,"���û�ע��",true);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	/**
	 * Create the dialog.
	 */
	public RegGUI() {
		this.setTitle("虚拟城市注册系统");
		setBounds(100, 100, 450, 300);
		//setModalityType(ModalityType.APPLICATION_MODAL);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNewLabel = new JLabel("\u7528\u6237\u540D\uFF1A");
			lblNewLabel.setBounds(42, 25, 54, 15);
			contentPanel.add(lblNewLabel);
		}
		{
			JLabel label = new JLabel("\u5BC6\u7801\uFF1A");
			label.setBounds(42, 53, 54, 15);
			contentPanel.add(label);
		}
		{
			JLabel label = new JLabel("\u786E\u8BA4\u5BC6\u7801\uFF1A");
			label.setBounds(42, 81, 70, 15);
			contentPanel.add(label);
		}
		{
			/*JLabel label = new JLabel("\u5BC6\u7801\u63D0\u793A\u95EE\u9898\uFF1A");
			label.setBounds(42, 112, 84, 15);
			contentPanel.add(label);*/
		}
		{
			/*JLabel label = new JLabel("\u95EE\u9898\u7B54\u6848\uFF1A");
			label.setBounds(42, 137, 70, 15);
			contentPanel.add(label);*/
		}
		{
			textField_ID = new JTextField();
			textField_ID.setBounds(136, 25, 66, 21);
			contentPanel.add(textField_ID);
			textField_ID.setColumns(10);
		}
		{
			textField_pw = new JPasswordField();
			textField_pw.setBounds(136, 53, 66, 21);
			contentPanel.add(textField_pw);
			textField_pw.setColumns(10);
		}
		{
			textField_pw2 = new JPasswordField();
			textField_pw2.setBounds(136, 81, 66, 21);
			contentPanel.add(textField_pw2);
			textField_pw2.setColumns(10);
		}
		/*{
			textField_question = new JTextField();
			textField_question.setBounds(136, 112, 66, 21);
			contentPanel.add(textField_question);
			textField_question.setColumns(10);
		}
		{
			textField_answer = new JTextField();
			textField_answer.setBounds(136, 137, 66, 21);
			contentPanel.add(textField_answer);
			textField_answer.setColumns(10);
		}*/
		
		JLabel label = new JLabel("\u771F\u5B9E\u59D3\u540D\uFF1A");
		label.setBounds(243, 25, 66, 15);
		contentPanel.add(label);
		
		JLabel label_1 = new JLabel("\u6027\u522B\uFF1A");
		label_1.setBounds(243, 53, 54, 15);
		contentPanel.add(label_1);
		
		textField_name = new JTextField();
		textField_name.setBounds(324, 22, 66, 21);
		contentPanel.add(textField_name);
		textField_name.setColumns(10);
		
		/*textField_sex = new JTextField();
		textField_sex.setBounds(324, 53, 66, 21);
		contentPanel.add(textField_sex);
		textField_sex.setColumns(10);*/
		
		final JComboBox c1 = new JComboBox();
		String[] items = {"男","女"};
		for(int i = 0; i < items.length; i++){
		   c1.addItem(items[i]);
		}
		c1.setBounds(324, 53, 66, 21);
		contentPanel.add(c1);

		
			
			
				okButton = new JButton("确认");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						String username=textField_ID.getText();
						String password=textField_pw.getText().toString();
						String password2=textField_pw2.getText().toString();
						String name=textField_name.getText();
						String sexString=(String) c1.getSelectedItem();
						
						if(!(username.equals("")||password.equals("")||password2.equals("")||name.equals("")||sexString.equals("")))
						{
							if(!password.equals(password2)){
								JOptionPane.showMessageDialog(null, "两次输入密码不相同！");
							}else{
							
							Configuration config=new Configuration();
							PersonInterface person=null;
							String rmiAddress=config.getRmiAddress();
							try {
								person = (PersonInterface) Naming.lookup(rmiAddress+"Person");
							} catch (MalformedURLException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} catch (RemoteException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							} catch (NotBoundException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							int pid=-1;
							try{
								if(person.hasName(username)){
									JOptionPane.showMessageDialog(null, "用户名"+username+"已存在！");
								}else{
							
									pid=person.reg(username,password,name,sexString);
							
							
									if(pid!=-1){
										JOptionPane.showMessageDialog(null, "注册成功！");
										Map dialog = new Map(pid);
										dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
										dialog.setVisible(true);
										dispose();
									}else{
										JOptionPane.showMessageDialog(null, "注册失败！");
									}
								}
							} catch (HeadlessException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
								} catch (RemoteException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
								} catch (MalformedURLException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								} catch (NotBoundException e1) {
									// TODO Auto-generated catch block
									e1.printStackTrace();
								}
							}
						}
						else
						{
							JOptionPane.showMessageDialog(null, "请填写完整注册信息");
						}
					}
				});
				okButton.setBounds(150, 200, 66, 21);
				okButton.setActionCommand("OK");
				contentPanel.add(okButton);
				getRootPane().setDefaultButton(okButton);
			
			
				cancelButton = new JButton("返回");
				cancelButton.setBounds(250, 200, 66, 21);
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) { 
						LogInGUI frame = new LogInGUI("虚拟城市登录");
						frame.setVisible(true);
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				contentPanel.add(cancelButton);
			
		
	}
}
